﻿using System.Diagnostics.CodeAnalysis;
using BlazorWebassemblyApp.DataModel;
using BootstrapBlazor.Components;
using Microsoft.AspNetCore.Components;

namespace BlazorWebassemblyApp.Pages;

public partial class StudentList
{
    #region 注入服务

    /// <summary>
    /// 获得 弹窗注入服务
    /// </summary>
    [Inject]
    [NotNull]
    private DialogService? dialogService { get; set; }

    #endregion

    #region 组件私有属性
    private static int currentPageNumber { get; set; }=1;
    private static IEnumerable<int> PageItemsSource => new int[] { 15, 30, 50, 100 };
    private List<string> needHiddenColumnList { get; set; } = new List<string>();

    #endregion

    /// <summary>
    /// 查询
    /// </summary>
    /// <param name="options"></param>
    /// <returns></returns>
    private async Task<QueryData<Student>> OnQueryAsync(QueryPageOptions options)
    {
        var studentList = Student.ConstructStudentList();

        currentPageNumber = options.PageIndex > 1 ? options.PageIndex : currentPageNumber;
        options.PageIndex=currentPageNumber;
        var studentItems = studentList.Skip((options.PageIndex - 1) * options.PageItems).Take(options.PageItems);

        return await Task.FromResult(new QueryData<Student>()
        {
            Items = studentItems,
            TotalCount = studentList.Count,
            IsSorted = true,
            IsFiltered = true,
            IsSearch = true,
            IsAdvanceSearch = true
        });
    }
    #region 组件私有方法

    /// <summary>
    /// 组件初始化方法
    /// </summary>
    protected override async Task OnInitializedAsync()
    {
        //todo-志军：同步给needHiddenColumnList赋值，页面能正常隐藏列；异步给needHiddenColumnList赋值，页面没法正常隐藏列
        //needHiddenColumnList = new List<string>() { "Name", "Age" };

        needHiddenColumnList =await Task.Run(() =>
        {
            return Task.FromResult(new List<string>() { "Name", "Age" });
        });

        await base.OnInitializedAsync();
    }

    private async Task OpenWindow()
    {
        var resultDialogOption = new ResultDialogOption()
        {
            Title ="学生详细信息",
            ShowFooter = false,
            Size = Size.Large
        };
        var result = await dialogService.ShowModal<StudentDetail>(resultDialogOption);
    }

    /// <summary>
    /// 是否显示当前列
    /// </summary>
    /// <param name="columnName"></param>
    /// <returns></returns>
    public bool IsShowColumn(string columnName)
    {
        var isShow = needHiddenColumnList.Contains(columnName) ? false : true;
        return isShow;
    }

    #endregion

}